#!/bin/sh
#
# $OpenBSD: fixup-hs-plist,v 1.1 2015/09/01 09:58:00 kili Exp $
#
# usage: fixup-hs-plist [plist ...]
#
# Fixes the given plists (default if no file given: pkg/PLIST) for
# hs-ports by
#
# - replacing all occurences of ${FULLPKGNAME[^}*]} by hs-${DISTNAME},
#   i.e. repair the broken backward substution made by update-plist,
# - ensures that the @exec .../register.sh, @unexec .../unregister.sh
#   and the lib/ghc/${DISTNAME}/unregister.sh come at the end of
#   the plist file, to stop the warnings about non-existing .haddock
#   files.

if [ $# -eq 0 ]; then
	set pkg/PLIST
fi

for plist; do
	cp -p "$plist" "$plist~"
	ed -s "$plist" <<- 'EOF'
		g/\${FULLPKGNAME[^}]*}/s/\${FULLPKGNAME[^}]*}/hs-${DISTNAME}/g
		g/^@exec .*%D\/lib\/ghc\/\${DISTNAME}\/register\.sh/d
		g/^@unexec .*%D\/lib\/ghc\/\${DISTNAME}\/unregister\.sh/d
		g/^lib\/ghc\/\${DISTNAME}\/unregister\.sh$/d
		$a
		@exec /usr/bin/env HOME=/nonexistent %D/lib/ghc/${DISTNAME}/register.sh -v0
		@unexec /usr/bin/env HOME=/nonexistent %D/lib/ghc/${DISTNAME}/unregister.sh -v0 --force
		lib/ghc/${DISTNAME}/unregister.sh
		.
		wq
	EOF
	[ $? -eq 0 ] || cat "$plist~" > "$plist"
	rm -f "$plist~"
done
